﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gpsys.Web.Models;
using Gpsys.Web.Common;

namespace Gpsys.Web.Controllers
{
    public class WorkflowController : BaseController
    {
        private void SetSelectedMenu()
        {
            ViewBag.SelectedMenu = "Workflow";
        }

        //
        // GET: /Workflow/

        public ActionResult Index()
        {
            SetSelectedMenu();
            WorkflowModel wfModel = new WorkflowModel();
            return View(wfModel);
        }

        public ActionResult Create()
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Workflow";
            ViewBag.IsUpdate = false;
            WorkflowEntityModel model = new WorkflowEntityModel()
            {
                Workflow = new Entity.MSG.Workflow()
            };
            return View("Edit", model);
        }

        [HttpPost]
        [ValidateInput(enableValidation: false)]
        public ActionResult Create(WorkflowEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Workflow";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = false;

            WorkflowModel wfModel = new WorkflowModel();

            wfModel.ValidateWorkflow(ModelState, model);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                model.Workflow.CreatedBy = UserHelpers.UserName;
                model.Workflow.ModifiedBy = UserHelpers.UserName;

                if (wfModel.InsertWorkFlow(model.Workflow))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        [HttpPost]
        [ValidateInput(enableValidation: false)]
        public ActionResult Edit(WorkflowEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Workflow";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = true;

            WorkflowModel wfModel = new WorkflowModel();

            wfModel.ValidateWorkflow(ModelState, model);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {

                model.Workflow.ModifiedBy = UserHelpers.UserName;

                if (wfModel.UpdateWorkFlow(model.Workflow))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        public ActionResult Edit(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Workflow";

            ViewBag.IsUpdate = true;

            WorkflowModel wfModel = new WorkflowModel();
            var wf = wfModel.GetWorkflow(Id);
            if (wf != null)
            {
                WorkflowEntityModel m = new WorkflowEntityModel()
                {
                    Workflow = wf
                };
                return View("Edit", m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Detail(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Detail Workflow";

            WorkflowModel wfModel = new WorkflowModel();
            var wf = wfModel.GetWorkflow(Id);
            if (wf != null)
            {
                WorkflowEntityModel m = new WorkflowEntityModel()
                {
                    Workflow = wf
                };
                return View(m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Delete(int Id)
        {
            SetSelectedMenu();

            WorkflowModel wfModel = new WorkflowModel();
            
            var wf = wfModel.GetWorkflow(Id);
            if (wf != null)
            {
                wfModel.DeleteWorkflow(Id);

                return RedirectToAction("Index");
            }

            return RedirectToAction("e404", "Error");
        }

    }
}
